<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form
        :model="queryParams"
        @submit.native.prevent
        ref="queryForm"
        size="small"
        :inline="true"
        class="clearfix"
        label-width="auto"
      >
        <el-form-item label="统计时段">
          <el-form-item label="" style="margin-bottom: 0px; margin-right: 5px">
            <el-radio-group v-model="queryParams.type" @change="changeTime" prop="type">
              <el-radio-button label="month">本月</el-radio-button>
              <el-radio-button label="year">今年</el-radio-button>
            </el-radio-group></el-form-item
          >
          <el-form-item label="" style="margin-bottom: 0px" prop="time">
            <el-date-picker
              style="width: 260px"
              v-model="queryParams.time"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-form-item label="" style="margin-bottom: 0px; margin-right: 20px" prop="contrast">
            <el-checkbox v-model="queryParams.contrast">对比时段</el-checkbox>
          </el-form-item>
          <el-form-item label="" style="margin-bottom: 0px" prop="timeContrast">
            <el-date-picker
              v-model="queryParams.timeContrast"
              type="daterange"
              style="width: 260px"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form-item>
        <el-form-item label="客户类型" prop="customerTypeList">
          <el-select
            v-model="queryParams.customerTypeList"
            collapse-tags
            multiple
            placeholder="请选择客户类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户标签" prop="customerTagList">
          <el-select
            v-model="queryParams.customerTagList"
            collapse-tags
            multiple
            placeholder="请选择客户标签"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_tag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value * 1"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" size="mini" plain @click="handleExport" icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-table v-loading="loading" :data="list" default-expand-all>
        <!-- <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expansionTime, '{y}-{m}-{d}') }}</span>
          </template> -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table :show-header="false" :data="props.row.childrens" style="width: 100%">
              <el-table-column width="48" align="center"></el-table-column>
              <el-table-column align="center" prop="date"></el-table-column>
              <el-table-column
                align="center"
                v-for="keys in [
                  'customerNum',
                  'signCustomerNum',
                  'signContractAmount',
                  'pendingPayment',
                  'expiredNotInvoiced',
                  'invoicedUnpaid',
                  'receivedPayment',
                  'unexpired',
                  'customerExpense',
                ]"
                :prop="keys"
              >
                <template slot-scope="scope">
                  <span
                    :style="
                      'color:' +
                      ((scope.$index === 1 && !scope.row[keys]) || scope.$index == 0
                        ? ''
                        : scope.$index === 1 && props.row.contrastData[keys] < props.row[keys]
                        ? '#CC3300'
                        : 'rgba(51, 171, 159, 0.8)') +
                      ''
                    "
                    ><i
                      v-if="scope.$index === 1"
                      :class="
                        !scope.row[keys] || scope.$index == 0
                          ? ''
                          : props.row.contrastData[keys] < props.row[keys]
                          ? 'el-icon-top'
                          : 'el-icon-bottom'
                      "
                    ></i
                    >{{ scope.row[keys] || 0 }}{{ scope.$index === 1 ? '%' : '' }}</span
                  >
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="月份" align="center" prop="date"> </el-table-column>
        <el-table-column label="客户数" align="center" prop="customerNum"> </el-table-column>
        <el-table-column label="签约客户数" align="center" prop="signCustomerNum"> </el-table-column>
        <el-table-column label="签约额(万元)" align="center" prop="signContractAmount"> </el-table-column>
        <el-table-column label="待回款(万元)" align="center" prop="pendingPayment"> </el-table-column>
        <el-table-column label="到期未开票(万元)" align="center" prop="expiredNotInvoiced"> </el-table-column>
        <el-table-column label="已开票未回款(万元)" align="center" prop="invoicedUnpaid"> </el-table-column>
        <el-table-column label="已回款（万元）" align="center" prop="receivedPayment"> </el-table-column>
        <el-table-column label="未到期（万元）" align="center" prop="unexpired"> </el-table-column>
        <el-table-column label="客情支出(万元)" align="center" prop="customerExpense"> </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { customerSummaryList } from '@/api/biz/index'
import dayjs from 'dayjs'
export default {
  name: 'projectSummary',
  dicts: ['project_type', 'project_level', 'project_progress', 'customer_type', 'customer_tag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 市场拓展表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerTag: '',
        customerTagList: [],
        customerType: '',
        customerTypeList: [],
        type: 'month',
        time: [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
        timeContrast: [
          dayjs().subtract(1, 'year').startOf('month').format('YYYY-MM-DD'),
          dayjs().subtract(1, 'year').format('YYYY-MM-DD'),
        ],
        contrast: true,
      },

      expansionId: null,
      showDetails: false,
      typeList: [],
      disableds: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeTime() {
      const { type, time } = this.queryParams
      if (type === 'month') {
        this.queryParams.time = [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
        if (this.queryParams.contrast) {
          this.queryParams.timeContrast = [
            dayjs().subtract(1, 'year').startOf('month').format('YYYY-MM-DD'),
            dayjs().subtract(1, 'year').format('YYYY-MM-DD'),
          ]
        }
      } else {
        this.queryParams.time = [dayjs().startOf('year').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
        if (this.queryParams.contrast) {
          this.queryParams.timeContrast = [
            dayjs().subtract(1, 'year').startOf('year').format('YYYY-MM-DD'),
            dayjs().subtract(1, 'year').format('YYYY-MM-DD'),
          ]
        }
      }
    },
    /** 查询列表 */
    getList() {
      const currentData = this.getParams()
      this.loading = true
      customerSummaryList(currentData)
        .then((response) => {
          const newList = response.data.map((item) => {
            if (this.queryParams.contrast) {
              item.childrens = [item.contrastData, item.ratio]
            }
            return item
          })
          this.list = newList
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        customerTag: '',
        customerTagList: [],
        customerType: '',
        customerTypeList: [],
        type: 'month',
        time: [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
        timeContrast: [
          dayjs().subtract(1, 'year').startOf('month').format('YYYY-MM-DD'),
          dayjs().subtract(1, 'year').format('YYYY-MM-DD'),
        ],
        contrast: true,
      }
      this.handleQuery()
    },
    getParams() {
      const currentData = JSON.parse(JSON.stringify(this.queryParams))
      const { time, contrast, timeContrast } = currentData
      if (time && time.length > 0) {
        currentData.beginDate = time[0]
        currentData.endDate = time[1]
      }
      if (contrast && timeContrast && timeContrast.length > 0) {
        currentData.beginDateContrast = timeContrast[0]
        currentData.endDateContrast = timeContrast[1]
      }
      delete currentData.time
      delete currentData.type
      delete currentData.contrast
      delete currentData.timeContrast
      return currentData
    },
    /** 导出按钮操作 */
    handleExport() {
      const currentData = this.getParams()
      this.download(
        '/biz/statistics/customer/customerSummaryListExport',
        currentData,
        `客户关系汇总统计列表导出_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
